import heapq, itertools
INVALID = -1
counter = itertools.count(1)    # unique sequence count

class PriorityQueue:

	def __init__(self):
		self.pq = []                         # the priority queue list
		self.data_finder = {}                # mapping of tasks to entries

	def enqueue(self, priority, data, parent, cost):
		count = next(counter)
   		entry = [priority, count, cost, data, parent]
		self.data_finder[data] = entry
		heapq.heappush(self.pq, entry)

	def dequeue(self):
		while self.pq:
			priority, count, cost, data, parent = heapq.heappop(self.pq)
			if data in self.data_finder:
				del self.data_finder[data]
			if count != INVALID:
				return priority, cost, data, parent

	def get_data_priority(self, data):
		if data in self.data_finder:
			entry = self.data_finder[data]
			if entry:
				return entry[0]
		return False
		
	def reprioritize(self, priority, data, parent, cost):
		entry = self.data_finder[data]
		self.enqueue(priority, data, parent, cost)
		entry[1] = INVALID

	def has_item(self):
		if (self.pq):
			return True
		else:
			return False
			
